package com.bdmc.web.controller.car;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.car.domain.bo.CarMaintenanceBo;
import com.bdmc.car.domain.bo.CarMaintenanceDetailBo;
import com.bdmc.car.domain.vo.CarMaintenanceVo;
import com.bdmc.car.service.ICarMaintenanceDetailService;
import com.bdmc.car.service.ICarMaintenanceService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 维修信息控制器
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/car/maintenance")
public class CarMaintenanceController extends BaseController {

    private final ICarMaintenanceService iCarMaintenanceService;

    private final ICarMaintenanceDetailService carMaintenanceDetailService;
    /**
     * 查询维修信息列表
     */
    @SaCheckPermission("car:maintenance:list")
    @GetMapping("/list")
    public TableDataInfo<CarMaintenanceVo> list(@Validated(QueryGroup.class) CarMaintenanceBo bo, PageQuery pageQuery) {
        return iCarMaintenanceService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出维修信息列表
     */
    @SaCheckPermission("car:maintenance:export")
    @Log(title = "维修信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated CarMaintenanceBo bo, HttpServletResponse response) {
        List<CarMaintenanceVo> list = iCarMaintenanceService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修信息", CarMaintenanceVo.class, response);
    }

    /**
     * 获取维修信息详细信息
     */
    @SaCheckPermission("car:maintenance:query")
    @GetMapping("/{maintenanceId}")
    public AjaxResult<CarMaintenanceVo> getInfo(@PathVariable("maintenanceId") Integer maintenanceId) {
        final CarMaintenanceVo carMaintenanceVo = iCarMaintenanceService.queryById(maintenanceId);
        final CarMaintenanceDetailBo detailBo = new CarMaintenanceDetailBo();
        detailBo.setMaintenanceId(maintenanceId);
        carMaintenanceVo.setCarMaintenanceDetailList(carMaintenanceDetailService.queryList(detailBo));
        return AjaxResult.success(carMaintenanceVo);
    }

    /**
     * 新增维修信息
     */
    @SaCheckPermission("car:maintenance:add")
    @Log(title = "维修信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody CarMaintenanceBo bo) {
        return toAjax(iCarMaintenanceService.insertByBo(bo));
    }

    /**
     * 修改维修信息
     */
    @SaCheckPermission("car:maintenance:edit")
    @Log(title = "维修信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody CarMaintenanceBo bo) {
        return toAjax(iCarMaintenanceService.updateByBo(bo));
    }

    /**
     * 删除维修信息
     */
    @SaCheckPermission("car:maintenance:remove")
    @Log(title = "维修信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{maintenanceIds}")
    public AjaxResult<Void> remove(@PathVariable Integer[] maintenanceIds) {
        return toAjax(iCarMaintenanceService.deleteWithValidByIds(Arrays.asList(maintenanceIds), true));
    }
}
